﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HZ.Supervise.Model;
namespace HZ.Supervise.Web.Secondphase.ReportHospital
{
    public partial class HospitalGoodsList : BasePage
    {
        int t = 0;
        string where = "";
        string order = "";
        public string DayTime;
        public string HospitalName;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
                SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
                if (!string.IsNullOrEmpty(sq.Mid)) //如果月份为空 参数有误
                {
                    if (sq.SortSource == 0)
                    {
                        droSort.SelectedIndex = 1;
                    }
                    else if (sq.SortSource == 1)
                    {
                        droSort.SelectedIndex = 2;
                    }
                    else if (sq.SortSource == 2)
                    {
                        droSort.SelectedIndex = 3;
                    }
                    else
                    {
                        droSort.SelectedIndex = -1;
                    }

                    BindList();
                }
                else
                {
                    Response.Write("<script>history.go(-1);<script>");
                }
            }
        }
        /// <summary>
        /// by张雄 条件 修改 数据改从静态表读取 
        /// 2011.5.12
        /// </summary>
        void BindList()
        {
            SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
            SuperviseQuery sqCopy = new SuperviseQuery();

            if (!string.IsNullOrEmpty(txtComPlainDate.Text.Trim()))
            {
                where += "and ProductName like '%" + txtComPlainDate.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txtJi.Text.Trim()))
            {
                where += "and MedicineModel like '%" + txtJi.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txtUnit.Text.Trim()))
            {
                where += "and Unit like '%" + txtUnit.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txtPack.Text.Trim()))
            {
                where += "and Pack like '%" + txtPack.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txtCompanyName_SC.Text.Trim()))
            {
                where += "and CompanyName_SC like '%" + txtCompanyName_SC.Text.Trim() + "%'";
            }

            
            where += " and DayTime like'%" + sq.Mid + "%'";
            where += "and HospitalName like '%" + sq.HospitalName + "%'";

            //判断SortSource是不是等于-1 等于-1则传入所有的采购类型  否则传入上一个相应的SortSource
            //if (sq.SortSource == -1)
            //{
            //    where += "and SortSource in (0,1,2)";
            //}
            //else 
            if (!droSort.SelectedValue.Equals("-1")) 
            {
                where += "and SortSource =" + droSort.SelectedValue;
            }

            order = droSortField.SelectedValue;
            HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();

            //将数据填入gridview中
            System.Data.DataTable dt = ie.ExecutePageNew(where, order, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out t);
            AspNetPager1.RecordCount = t;
            HZ.Supervise.Web.Function.ChangeRate(dt, "WareHouseRate");
            HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DistributeRate");
            gvwGoods.DataSource = dt;
            gvwGoods.DataBind();
        }

        protected void gvwGoods_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //e.Row.Cells[12].Text = " <a href=\"HospitalGoodsDetail.aspx?mid=" + strMosth + "&hname=" + strhospitalname + "&gid=" + Server.UrlEncode(e.Row.Cells[1].Text.Trim()) + "\">详情</a>";
            }
        }

        protected void gvwGoods_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DirectByDetail")
            {
                string sessionid = Guid.NewGuid().ToString();
                SuperviseQuery query = (SuperviseQuery)ViewState["query"];
                try
                {
                    query.ProcureCatalogID = e.CommandArgument.ToString();
                }
                catch { }
                Session[sessionid] = query;
                string url = "HospitalGoodsDetail.aspx";
                Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
        }

        /// <summary>
        /// 合并表头
        /// create by张雄 2011.5.5
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void gvwGoods_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.Header:
                    //取得月份
                    //第一行表头
                    TableCellCollection tc = e.Row.Cells;
                    tc.Clear();
                    tc.Add(new TableHeaderCell());
                    tc[0].Attributes.Add("colspan", "13"); //跨Column
                    tc[0].Attributes.Add("bgcolor", "LightSteelBlue");
                    SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
                    tc[0].Text = HZ.Supervise.Web.Function.FormartDate(sq.Mid) + " " + sq.HospitalName + "</th></tr><tr>";

                    //第2行表头
                    tc.Add(new TableHeaderCell());
                    tc[1].Attributes.Add("bgcolor", "Khaki");
                    tc[1].Text = "序号";

                    tc.Add(new TableHeaderCell());
                    tc[2].Attributes.Add("bgcolor", "Khaki");
                    tc[2].Text = "通用名";

                    tc.Add(new TableHeaderCell());
                    tc[3].Attributes.Add("bgcolor", "Khaki");
                    tc[3].Text = "剂型";

                    tc.Add(new TableHeaderCell());
                    tc[4].Attributes.Add("bgcolor", "Khaki");
                    tc[4].Text = "采购规格";

                    tc.Add(new TableHeaderCell());
                    tc[5].Attributes.Add("bgcolor", "Khaki");
                    tc[5].Text = "采购单位";

                    tc.Add(new TableHeaderCell());
                    tc[6].Attributes.Add("bgcolor", "Khaki");
                    tc[6].Text = "采购类型";

                    tc.Add(new TableHeaderCell());
                    tc[7].Attributes.Add("bgcolor", "Khaki");
                    tc[7].Text = "生产企业";

                    tc.Add(new TableHeaderCell());
                    tc[8].Attributes.Add("bgcolor", "Khaki");
                    tc[8].Text = "采购金额";

                    tc.Add(new TableHeaderCell());
                    tc[9].Attributes.Add("bgcolor", "Khaki");
                    tc[9].Text = "配送金额";

                    tc.Add(new TableHeaderCell());
                    tc[10].Attributes.Add("bgcolor", "Khaki");
                    tc[10].Text = "配送率";

                    tc.Add(new TableHeaderCell());
                    tc[11].Attributes.Add("bgcolor", "Khaki");
                    tc[11].Text = "入库金额";

                    tc.Add(new TableHeaderCell());
                    tc[12].Attributes.Add("bgcolor", "Khaki");
                    tc[12].Text = "入库率";

                    tc.Add(new TableHeaderCell());
                    tc[13].Attributes.Add("bgcolor", "Khaki");
                    tc[13].Text = "操作";
                    break;
            }
        }
        /// <summary>
        /// 格式化日期
        /// create by张雄   2011.5.5
        /// </summary>
        /// <param name="str">日期参数</param>
        /// <returns>格式化后的日期</returns>
        private string ForMatDate(string str)
        {
            string YearStr = String.Empty;
            string MonthStr = String.Empty;

            if (!string.IsNullOrEmpty(str))
            {
                if (str.Length == 6)
                {
                    //获取年的值
                    YearStr = str.Substring(0, 4) + "年";
                    MonthStr = str.Substring(4, 2);     //获取月的值
                    if (MonthStr.Substring(0, 1) == "0")    //判断月 如果第一位是0 则显示一位
                    {
                        MonthStr = MonthStr.Substring(1, 1) + "月";
                    }
                    else
                    {
                        MonthStr += "月";
                    }
                }
            }
            return str = YearStr + MonthStr;
        }

        /// <summary>
        /// 分页事件
        /// by张雄  2011.5.13
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindList();
        }

        /// <summary>
        /// 导出数据
        /// by张雄  2011.5.13
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
            SuperviseQuery sqCopy = new SuperviseQuery();

            if (!string.IsNullOrEmpty(txtComPlainDate.Text.Trim()))
            {
                where += "and ProductName like '%" + txtComPlainDate.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txtJi.Text.Trim()))
            {
                where += "and MedicineModel like '%" + txtJi.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txtUnit.Text.Trim()))
            {
                where += "and Unit like '%" + txtUnit.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txtPack.Text.Trim()))
            {
                where += "and Pack like '%" + txtPack.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txtCompanyName_SC.Text.Trim()))
            {
                where += "and CompanyName_SC like '%" + txtCompanyName_SC.Text.Trim() + "%'";
            }

            where += " and DayTime like'%" + sq.Mid + "%'";
            where += "and HospitalName like '%" + sq.HospitalName + "%'";

            order = droSortField.SelectedValue;
            HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();

            //将数据填入gridview中
            System.Data.DataTable dt = ie.ExecutePageNew(where, order, 1, 0, out t);
            AspNetPager1.RecordCount = t;
            HZ.Supervise.Web.Function.ChangeRate(dt, "WareHouseRate");
            HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DistributeRate");
            gvwGoods.DataSource = dt;
            gvwGoods.DataBind();
            HZ.Excel.ExcelHelper.ExportDataForWeb(dt, this.gvwGoods);
        }
    }
}
